package com.hq.modeapp.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hq.modeapp.R;
import com.hq.modeapp.bean.News_Bean;
import com.hq.modeapp.bean.Photo_Bean;
import com.hq.modeapp.bean.Photo_List_Bean;
import com.hq.modeapp.manager.AppContext;
import com.hq.modeapp.ui.PhotoViewFlowActivity;

public class NewsListViewAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private ArrayList<News_Bean> list = new ArrayList<News_Bean>();
	private Context context;
	private AppContext appContext;

	public NewsListViewAdapter(Context context,
			ArrayList<News_Bean> list) {

		this.mInflater = LayoutInflater.from(context);
		this.context = context;
		this.appContext = (AppContext)(context.getApplicationContext());
		this.setList(list);

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(list.size()==0){
			return 0;
		}
		return getList().size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return getList().get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;

		final News_Bean bean = getList().get(position);

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.list_item, null);
			holder.tv_title=(TextView) convertView.findViewById(R.id.tv_title);
			holder.img_imgsrc=(ImageView) convertView.findViewById(R.id.img_imgsrc);
			holder.tv_digest=(TextView) convertView.findViewById(R.id.tv_digest);
			holder.tv_replyCount=(TextView) convertView.findViewById(R.id.tv_replyCount);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tv_title.setText(null);
		holder.tv_digest.setText(null);
		holder.tv_replyCount.setText(null);
		appContext.finalBitmap.display(holder.img_imgsrc, null);
		
		holder.tv_title.setText(bean.getTitle());
		holder.tv_digest.setText(bean.getDigest());
		holder.tv_replyCount.setText(bean.getReplyCount()+"跟帖");
		appContext.finalBitmap.display(holder.img_imgsrc, bean.getImgsrc());
		System.out.println("图片url="+bean.getImgsrc());
		return convertView;
	}

	/**
	 * @BareFieldName : list
	 * 
	 * @return the list
	 * 
	 * 
	 **/

	public ArrayList<News_Bean> getList() {
		return list;
	}

	/**
	 * @BareFieldName : list
	 * 
	 * @return the list
	 * 
	 * @param list
	 *            the list to set
	 * 
	 **/

	public void setList(ArrayList<News_Bean> list) {
		this.list.clear();
		this.list.addAll(list);
	}

	// �Զ���������
	class ViewHolder {
		 TextView tv_title;
		 ImageView img_imgsrc;
		 TextView tv_digest;
		 TextView tv_replyCount;

	}

	/**
	 * **********************************************************
	 * 
	 * @����ժҪ �������ղء�ɾ��ť�ļ���
	 * 
	 * @���� ��Administrator
	 * @����ʱ�� ��2012-11-1 ����2:46:45
	 * @��ʷ��¼ :
	 * @���� : 2012-11-1 ����2:46:45 �޸��ˣ�����
	 * @���� :
	 *********************************************************** 
	 */
	class MyOnClickListener implements OnClickListener {
		private ArrayList<Photo_Bean> list;
		private Photo_Bean lastPhoto;

		public MyOnClickListener() {
			super();
		}

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.img_item:

				Photo_List_Bean bean = (Photo_List_Bean) v.getTag();
				ImageView img_item = (ImageView) v;
				Intent intent = new Intent();
				intent.setClass(context, PhotoViewFlowActivity.class);
				intent.putExtra("ID", bean.getSetid());
				System.out.println("ID==="+bean.getSetid());
				context.startActivity(intent);
			default:
				break;
			}
		}
	}
}
